package org.mozhu.zufang.system.account.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mozhu.mboot.core.data.entity.Activatable;
import org.mozhu.mboot.core.data.entity.AuditingEntity;
import org.mozhu.mboot.core.data.entity.BaseEntity;
import org.mozhu.mboot.core.data.entity.LogicDeleteable;

import javax.persistence.*;

@Entity
@Table(name = "sys_user")
@Data
@EqualsAndHashCode(callSuper = true)
public class User extends AuditingEntity implements LogicDeleteable, Activatable {

    private static final long serialVersionUID = 239230219104716120L;

    // -- 必填项 -- //
    // 姓名
    protected String name;

    // 英文名
    protected String enName;

    // 登录名
    protected String loginName;

    // 不持久化到数据库，也不显示在Restful接口的属性.
    @Transient
    private String plainPassword;

    // 登录密码
    protected String password;

    // 加密/解密时的salt
    protected String salt;
    // 用户邮箱
    protected String email;

    protected String mobile;

    // 性别
    @Enumerated(EnumType.STRING)
    protected Gender gender;

    /**
     * 逻辑删除flag
     */
    private Boolean deleted = Boolean.FALSE;

    @Override
    public Boolean getDeleted() {
        return deleted;
    }

    @Override
    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public void markDeleted() {
        this.deleted = Boolean.TRUE;
    }

    /**
     * 激活(TRUE)，冻结(FALSE)
     */
    private Boolean activated = Boolean.TRUE;

    @Override
    public Boolean getActivated() {
        return activated;
    }

    @Override
    public void setActivated(Boolean activated) {
        this.activated = activated;
    }

    @Override
    public void markActivated() {
        this.activated = Boolean.TRUE;
    }

    @Override
    public void markDeactivated() {
        this.activated = Boolean.FALSE;
    }

    public boolean isAvailable() {
        return !Boolean.TRUE.equals(this.deleted) && !isBlocked();
    }

    public boolean isBlocked() {
        return !Boolean.TRUE.equals(getActivated());
    }

    @Override
    public void recycle() {
        setDeleted(Boolean.FALSE);
    }
}
